<template>
	<view class="page-profile">
		<view class="background-wrap">
			<image :src="club.background_image" mode="aspectFill" class="background"></image>
			<view class="base-info">
				<text class="name">{{ club.name }}</text>
				<image class="avatar" mode="aspectFill" :src="club.avatar"></image>
			</view>
		</view>
		<view class="actions">
			<view class="want">
				<text class="description">想去</text>
				<text class="amount">{{club.users_want_appoint_count}}</text>
				<icon class="iconfont icon-heart" :class="{had: hasWant}" @tap="doLike" :data-key="club.key" data-target_type="3" data-like_type="1"></icon>
			</view>
			<view class="done">
				<text class="description">去过</text>
				<text class="amount">{{club.users_had_appoint_count}}</text>
				<icon class="iconfont icon-oksign" :class="{had: hasHad}"  @tap="doLike" :data-key="club.key" data-target_type="3" data-like_type="2"></icon>
			</view>
		</view>
		<view class="key-info">
			<view v-for="(indexData, indexIndex) in club.key_indexes" :key="indexIndex" class="item text-one-line">
				<text class="label">{{ indexData.label }}</text>
				<text class="value text-one-line">{{ indexData.value }}</text>
			</view>
		</view>
		<view class="division"></view>
		<view class="tab-header" v-if="tab == 'report'">
			<view class="item" data-tap="report">
				<text class="active">Report</text>
			</view>
			<view class="item" data-tap="album">
				<text>Album</text>
			</view>
		</view>
		<view class="tab-content">
			<view v-for="item in technicians" :key="item.key" class="item" @tap="gotoTechnicianProfile" :data-key="item.key" :data-id="item.id">
				<view class="left">
					<view class="content-wrap">
						<text class="content">{{ item.topic.main_floor.detail.content }}</text>
					</view>
					<view class="meta-wrap text-one-line">
						<text>{{ item.name }}</text>
						<text>{{ item.short_name }}</text>
						<text>{{ item.index }}</text>
					</view>
				</view>
				<view class="right" v-if="item.avatar">
					<image :src="item.avatar" mode="aspectFill" class="picture"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniIcon from '@dcloudio/uni-ui/lib/uni-icon/uni-icon.vue'
	import ListOptions from '../../minxins/list-options.js'
	export default {
		data() {
			return {
				page: 'profile-club',
				club: {},
				hasWant: false,
				hasHad: false,
				technicians: [],
				tab: ""
			};
		},
		mixins: [ListOptions],
		async onLoad(options) {
			this.options = Object.assign({}, this.options, options)
			try {
				let result = await this.fetchPageData()
				this.renderPage(result)
			} catch (error) {
				
			}
		},
		methods: {
			gotoTechnicianProfile(e) {
				console.log(e)
				let dataset = e.currentTarget.dataset
				uni.navigateTo({
					url: '/pages/profile/technician?id=' + dataset.id,
				});
			},
			async doLike(e) {
				console.log(e)
				let dataset = e.currentTarget.dataset
				let result = await this.$api.createLike({
					key: dataset.key, 
					target_type: dataset.target_type, 
					like_type: dataset.like_type,
				})
				console.log(result)
				if (result.ret == 0) {
					if (dataset.like_type == 1) {
						this.hasWant = true
						this.club.users_want_appoint_count = result.data.like_count
					} else if (dataset.like_type == 2) {
						this.hasHad = true
						this.club.users_had_appoint_count = result.data.like_count
					}
				}
			},
			async fetchPageData() {
				let params = this.options;
				if (!params.id) {
					uni.showToast({
						title: '无ID'
					})
					return
				}
				params.__resource_id = params.id
				let result = await this.$api.getClub(params)
				return result;
			},
			async renderPage(result) {
				if (result.ret == 0) {
					this.club = result.data.data
					this.hasWant = result.data.has_want
					this.hasHad = result.data.has_had
					//再取Technician
					this.renderTechnician()
				}
			},
			async renderTechnician() {
				let __with = ['user', 'topic', 'topic.mainFloor', 'topic.mainFloor.detail', 'topic.mainFloor.detail.attachments']
				let params = {
					club_id: this.options.id,
					detail_excerpt: 1,
					page: this.options.page
				}
				let result = await this.$api.listTechnician(params)
				if (result['ret'] == 0) {
					if (result.data.data.length == 0) {
					  this.options.has_more = false;
					} else {
					  this.options.page++;
					  if (result.data.meta.current_page == 1) {
							this.technicians = result.data.data;
					  } else {
							this.technicians = [...this.technicians, ...result.data.data];
					  }
					}
				}
			}
			
		}
	}
</script>

<style lang="scss">
.page-profile {
	.background-wrap {
		position: relative;
		.background {
			height: 38vh;
			width: 100%;
		}
		.base-info {
			display: flex;
			position: absolute;
			bottom: -40upx;
			right: 40upx;
			align-items: center;
			justify-content: flex-end;
			.avatar {
				max-width: 160upx;
				max-height: 160upx;
			}
			.name {
				color: white;
				margin: 30upx;
			}
		}
	}
	.actions {
		display: flex;
		justify-content: center;
		align-content: center;
		width: calc(100% - 200upx);
		.done, .want {
			display: flex;
			flex-grow: 1;
			align-items: center;
			justify-content: center;
			.amount, .description {
			}
			.amount {
				margin: 0 10upx;
			}
			.iconfont {
				font-size: 50upx;
			}
			.had {
				color: #FF0033;
			}
		}
	}
	.key-info {
		display: flex;
		padding: 20upx 0;
		.item {
			display: flex;
			flex-grow: 1;
			justify-content: center;
			align-content: center;
			.label {
				color: #999;
				margin-right: 16upx;
				font-size: 28upx;
			}
			.value {
				
			}
		}
	}
	.division {
		width: 100%;
		height: 10upx;
		color:#eee;
		background-color: #eee;
	}
	.tab-header {
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 20upx;
		.item {
			flex-grow: 1;
			display: flex;
			justify-content: center;
			align-content: center;
			.active {
				border-bottom: 4upx solid orange;
			}
		}
		
	}
	.tab-content {
		.item {
			&:not(:last-child) {
				border-bottom: 2upx solid #eee;
			}
			justify-content: space-between;
			display: flex;
			flex-direction: row;
			padding: 20upx 30upx;
			.left {
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				.content-wrap {
					.content {
						overflow : hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
					}
				}
				.meta-wrap {
					text {
						font-size: 28upx;
						color: #aaa;
						margin-right: 10upx;
					}
				}
			}
			.right {
				margin-left: 20upx;
				.picture {
					width: 240upx;
					height: 160upx;
				}
			}
		}
		
	}
}
</style>
